<?php

namespace app\admin\model\course;

use think\Model;


class Transfer extends Model
{





    // 表名
    protected $name = 'course_transfer_school';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        "createtime_text",
        "type_text",
        "status_text",
    ];

    const TYPE_TEACHER = 1;// 换老师
    const TYPE_SCHOOL = 2;// 换学校
    public function getTypeList(){
        return [
            self::TYPE_TEACHER => __("转老师"),
            self::TYPE_SCHOOL => __("转校"),
        ];
    }


    const STATUS_APPLY = 1;// 申请中
    const STATUS_REFUSE = -1;// 拒绝
    const STATUS_SUCCESS = 2;// 完成
    public function getStatusList(){
        return [
            self::STATUS_APPLY => __("申请中"),
            self::STATUS_REFUSE => __("拒绝"),
            self::STATUS_SUCCESS => __("完成"),
        ];
    }



    public function getCreatetimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['createtime']) ? $data['createtime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i", $value) : $value;
    }

    public function getTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['type']) ? $data['type'] : '');
        $list = $this->getTypeList();
        return isset($list[$value]) ? $list[$value] : "";
    }
    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : "";
    }







    public function student()
    {
        return $this->belongsTo('app\admin\model\user\Student', 'student_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function applyTransfer($data){
        //检测有没有申请过
        $check = $this->where([
            "status" => $this::STATUS_APPLY,
            "student_id" => $data['student_id'],
            "order_id" => $data['order_id'],
        ])->find();
        if($check){
            exception("您已经提交过了,请勿重复提交");
        }
        $data['status'] = $this::STATUS_APPLY;
        $this->save($data);
    }

    /*
     * 申请列表
     * */
    public function transferList($condition){
        $list = $this->with(["student"=>function($query){
            $query->withField("name");
        }])->where($condition)->order("id","desc")->paginate();
        return $list;
    }

    public function transferInfo($condition){
        $info = $this->with(["student"=>function($query){
            $query->withField("name");
        }])->where($condition)->find();
        if(!$info){
            exception("未找到当前申请");
        }
        $condition = [
            "order.id" => $info['order_id'],
        ];
        $orderModel = new Order();
        $orderInfo = $orderModel->getOrderInfo($condition);



        $info['orderInfo'] = $orderInfo;
        return $info;
    }


    /*
        * 审核通过
        * */
    public function successApply($orderInfo,$newOrderInfo){
//        dump($newOrderInfo);exit;
        $this->status = self::STATUS_SUCCESS;

        $class_hour = $orderInfo['class_hour']??0;
        $surplus_class_hour = $orderInfo['surplus_class_hour']??0;

        $this->old_class_hour =$class_hour;
        $this->old_use_class_hour = $class_hour - $surplus_class_hour;
        $this->old_surplus_class_hour = $surplus_class_hour;
        $this->old_surplus_price = $orderInfo['surplus_price'];

        $new_class_hour = $newOrderInfo['class_hour']??0;
        $new_surplus_class_hour = $newOrderInfo['surplus_class_hour']??0;

        $this->new_class_hour =$new_class_hour;
        $this->new_use_class_hour = $new_class_hour - $new_surplus_class_hour;
        $this->new_surplus_class_hour = $new_surplus_class_hour;
        $this->new_surplus_price = $newOrderInfo['surplus_price'];
        $this->new_course_id = $newOrderInfo['course']['id']??0;
        $this->new_course_name = $newOrderInfo['course']['name']??"";
        $this->new_school_id = $newOrderInfo['school_id']??0;
        $this->new_school_name = $newOrderInfo['school_name']??'';

        $this->save();


    }

    /*
     * 拒绝申请
     * */
    public function refuseApply($refuse_content){


        $this->refuse_content = $refuse_content;
        $this->status = self::STATUS_REFUSE;
        $this->save();
    }
}
